using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace My.IdlConverter.Core.Util
{
    public static class IOHelper
    {
        public delegate bool PathNameFilterFun(string pathName);

        public static bool ReadToEnd(string filePath,out string content)
        {
            content = "";

            Stream stream = null;
            try
            {
                stream = File.OpenRead(filePath);
                TextReader reader = new StreamReader(stream);
                content = reader.ReadToEnd();
                return true;
            }
            catch (IOException ioe)
            {
                Trace.Write(ioe.Message);
                return false;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
        }

        public static StringCollection ReadLines(string filePath)
        {
            Stream stream = null;
            StringCollection lines = new StringCollection();
            try
            {
                stream = File.OpenRead(filePath);
                StreamReader reader = new StreamReader(stream);
                while (!reader.EndOfStream)
                {
                    lines.Add(reader.ReadLine());
                }
            }
            catch (IOException ioe)
            {
                Debug.Assert(false);
                Trace.Write(ioe.Message);
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return lines;
        }

        public static StringCollection GetFiles(string pathName, PathNameFilterFun filter, bool recursive, string searchPattern)
        {
            SearchOption option = SearchOption.AllDirectories;
            if (!recursive)
                option = SearchOption.TopDirectoryOnly;

            string[] files = Directory.GetFiles(pathName, searchPattern, option);
            Debug.Assert(files.Length > 0);
            StringCollection retFiles = new StringCollection();
            foreach (string nextFile in files)
            {
                if (filter != null)
                {
                    if (filter(nextFile))
                    {
                        retFiles.Add(nextFile);
                    }
                }
                else
                {
                    retFiles.Add(nextFile);
                }
            }

            return retFiles;
        }

        public static StringCollection GetCppHeaderFiles(string pathName,PathNameFilterFun filter,bool recursive)
        {
            return GetFiles(pathName, filter, recursive, "*.h");
        }

        public static StringCollection GetCppSourceFiles(string pathName, PathNameFilterFun filter, bool recursive)
        {
            return GetFiles(pathName, filter, recursive, "*.cpp");
        }
    }
}
